package org.practice.dao;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.practice.model.Product;
import org.practice.util.DaoException;
import org.practice.util.HibernateSessionFactory;

public class ProductDaoImpl implements ProductDao {
	private static Logger logger = Logger.getLogger(ProductDaoImpl.class);
	
	public Product findProductById(Integer id){
			Session session = null;
			try {
				session = HibernateSessionFactory.openSession();
				Query query = session
						.createQuery("select new Product(p.id, p.name, p.fixedPrice, p.dangPrice) from Product p where p.id=?");
				query.setParameter(0, id);
				List<Product> list = query.list();
				if (list.size() > 0) {
					return list.get(0);
				}
				return null;
			} catch (HibernateException e) {
				logger.error("��ݷ��ʳ���", e);
				throw new DaoException("��ݷ��ʳ���", e);
			} finally {
				HibernateSessionFactory.closeSession();
			}
			
	}

}
